function formatNumber (n) {
  const str = n.toString()
  return str[1] ? str : `0${str}`
}

export function formatTime (date) {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  const t1 = [year, month, day].map(formatNumber).join('/')
  const t2 = [hour, minute, second].map(formatNumber).join(':')

  return `${t1} ${t2}`
}

export function objKeySort(arys) {
  //先用Object内置类的keys方法获取要排序对象的属性名，再利用Array原型上的sort方法对获取的属性名进行排序，newkey是一个数组
  var newkey = Object.keys(arys).sort(function(s,t){
    var a = s.toLowerCase();
    var b = t.toLowerCase();
    if(a < b) return -1;
    if(a > b) return 1;
    return 0;
  })
  var newObj = {}; //创建一个新的对象，用于存放排好序的键值对
  for (var i = 0; i < newkey.length; i++) {
    //遍历newkey数组
    newObj[newkey[i]] = arys[newkey[i]];
    //向新创建的对象中按照排好的顺序依次增加键值对
  }
  return newObj; //返回排好序的新对象
}

export function toLogin() {
  const userInfo = wx.getStorageSync('userInfo');
  if (!userInfo) {
    wx.reLaunch({
      url: "/pages/login/main"
    });
  } else {
    return true;
  }
}

export function login() {
  const userInfo = wx.getStorageSync('userInfo');
  if (userInfo) {
    return userInfo;
  }
}

export function showToast(msg) {
  setTimeout(() => {
    wx.showToast({
      title: msg,
      icon: 'none',
      duration: 2000,
      mask: true
    })
  },500)
}
export function canvasArea(list,operateAreaId){

  let _this = this
  let areaArr = []
  list.forEach((item) => {
    let arr = []
    if(operateAreaId){
      if(item.operateAreaId == operateAreaId){
        if (item.regionLocations) {
          item.regionLocations.forEach((itemlocal) => {
            arr.push({ longitude: itemlocal.location[0], latitude: itemlocal.location[1] })
          })
          areaArr.push(arr)
        }
      }
    }else{
      if(item.operateAreaId){
        if (item.regionLocations) {
          item.regionLocations.forEach((itemlocal) => {
            arr.push({ longitude: itemlocal.location[0], latitude: itemlocal.location[1] })
          })
          areaArr.push(arr)
        }
      }
    }
    }
    )
  return areaArr
}

export function getUrlParam(url){
  let arr = url.split("?")[1].replace(/=/g,":").split('&');
  let obj = {};
　for (var i = 0; i < arr.length; i++) {
　  var split = arr[i].split(':');
　　obj[split[0]] = split[1];
　}
  return obj;
};

